//
//  CommentController.m
//  Product_NEWS
//
//  Created by dllo on 16/1/19.
//  Copyright © 2016年 FCH. All rights reserved.
//

#import "CommentController.h"

#import "CommentCell.h"
#import "CommentModel.h"
@interface CommentController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, retain) UITableView *myTableView;
@property (nonatomic, retain) NSMutableArray *dataSource;
@property (nonatomic, assign) BOOL isUpLoading;
@property (nonatomic, assign) NSInteger count;
@property (nonatomic, retain) MBProgressHUD *hud;
@end

@implementation CommentController
- (void)dealloc {
    [_docid release];
    [_flag release];
    [_dataSource release];
    [_myTableView release];
    [super dealloc];
}





- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        self.dataSource = [NSMutableArray array];
    }
    return [[_dataSource retain] autorelease];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createSubView];
     [self addHeader];
    [self addFooter];
     [self loading];
}



-(void)loading{
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    
     self.hud.labelText = @"正在加载";
    [self.hud showAnimated:YES whileExecutingBlock:^{
        sleep(1);
        
        [self GetPersonList:0];
    } completionBlock:^{
        
        [self.hud removeFromSuperview];
        [self.hud release];
        self.hud = nil;
    }];
    
        [self.view addSubview:self.hud];
    
}

-(void)createSubView
{
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, 375 * SCREEN_WIDTH, 667 * SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    
    self.myTableView.dataSource = self;
    self.myTableView.delegate  = self;
    
    [self.myTableView registerClass:[CommentCell class] forCellReuseIdentifier:@"comment"];
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.title = @"个性评论区";
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(handleLeft:)];
    self.navigationItem.leftBarButtonItem = leftItem;
    self.navigationController.automaticallyAdjustsScrollViewInsets  = NO;
     self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

}

#pragma mark - UITableViewDataSource, UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"comment"];
    //去掉cell的点击效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CommentModel *model = self.dataSource[indexPath.row];
  
    cell.model = model;
 
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CommentModel *model = self.dataSource[indexPath.row];
    return [CommentCell heightForRowWithDic:model];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 1;
}

#pragma mark - 数据配置
-(void)GetPersonList:(NSInteger)nextCursor
{
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    
    NSString *getstr = [NSString stringWithFormat:@"http://comment.api.163.com/api/json/post/list/new/normal/%@/%@/desc/%ld/10/10/2/2",self.flag, self.docid, self.count];

     [mgr GET:getstr parameters:dic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
         if (self.isUpLoading == NO) {
             //说明是下拉，就要清空数组中的数据
             [self.dataSource removeAllObjects];
         }

        
        NSMutableArray *dataArr = responseObject[@"newPosts"];
        
       
            for (NSDictionary *bigDict in dataArr) {
                NSDictionary *dict = bigDict[@"1"];
                CommentModel *model =[CommentModel new];
                [model setValuesForKeysWithDictionary:dict];
                [self.dataSource addObject:model];
               
                
                [self.myTableView footerEndRefreshing];
                [self.myTableView headerEndRefreshing];
                [self.myTableView reloadData];
        }

        
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        [self.myTableView headerEndRefreshing];
        [self.myTableView footerEndRefreshing];
        MBProgressHUD *mb = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        mb.mode = MBProgressHUDModeText;
        mb.labelText = @"网络不给力";
        [mb hide:YES afterDelay:0.5];
    }];
}

-(void)addHeader
{
    __block CommentController *vc = self;
    [self.myTableView addHeaderWithCallback:^{
        vc.count = 0;
        vc.isUpLoading = NO;
        [vc GetPersonList:vc.count];
    }];
//    [self.myTableView headerBeginRefreshing];
}

-(void)addFooter
{
    __block CommentController *vc = self;
    [self.myTableView addFooterWithCallback:^{
       vc.count += 11;
        vc.isUpLoading  = YES;
        
        [vc GetPersonList:vc.count];
    }];
    
    
}




#pragma mark - action

//点击返回按钮
- (void)handleLeft:(UIBarButtonItem *)item {
    if ([self.flag isEqualToString:@"photoview_bbs"]) {
        self.navigationController.navigationBarHidden = YES; //判断是不是ImagesDisplayController push过来的，如果是，pop之前将导航条隐藏
    }
    [self.navigationController popViewControllerAnimated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
